package com.open.party.cloud.view.home.jiCengDangJian.partyService

import android.os.Bundle
import android.widget.RadioButton
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppTitleBaseActivity
import com.open.party.cloud.R
import com.open.party.cloud.model.VolunteerMemberEntity
import com.open.party.cloud.viewModel.ZiYuanZheViewModel
import com.sinothk.android.utils.XUtils
import kotlinx.android.synthetic.main.activity_zyz_tuan_dui_join.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 */
class ZyzTuanDuiJoinActivity : AppTitleBaseActivity() {

    private var ziYuanZheVM: ZiYuanZheViewModel? = null

    private var volunteer: VolunteerMemberEntity? = null

    override fun getLayoutResId(): Int = R.layout.activity_zyz_tuan_dui_join

    override fun onResume() {
        super.onResume()
        EventBus.getDefault().register(this)
    }

    override fun onPause() {
        super.onPause()
        EventBus.getDefault().unregister(this)
    }

    private var teamId = ""
    private var title = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        teamId = intent.getStringExtra("id") as String
        title = intent.getStringExtra("title") as String

        setViewTitle(XUtils.string().getNotNullValue(title, "档案信息"))

        ziYuanZheVM = ZiYuanZheViewModel()

        initView()
    }

    private fun initView() {
        ziYuanZheVM!!.getZyzLoginUserInfo()

        submitBtn.setOnClickListener { doSubmit(volunteer!!) }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(volunteer: VolunteerMemberEntity?) {
        hideLoadingDialog()

        this.volunteer = volunteer
        if (volunteer == null) {
            return
        }

        // 加载已有数据
        userNameEt.setText(XUtils.string().getNotNullValue(volunteer.name))
        XUtils.view().focusMoveToEnd(userNameEt)
        phoneNumEt.setText(XUtils.string().getNotNullValue(volunteer.phone))

        // 性别
        if (volunteer.sex == null || volunteer.sex == "男") {
            maleRb.isChecked = true
        } else {
            femaleRb.isChecked = true
        }
    }

    private fun doSubmit(volunteer: VolunteerMemberEntity) {

        // 姓名
        val userName = userNameEt.text.toString()
        if (XUtils.string().isEmpty(userName)) {
            XUtils.toast().show("请正确输入姓名")
            return
        } else {
            volunteer.name = userName
        }
        // 手机号码
        val phoneNum = phoneNumEt.text.toString()
        if (XUtils.string().isEmpty(phoneNum)) {
            XUtils.toast().show("请输入手机号码")
            return
        }
        if (phoneNum.length != 11 || '1' != phoneNum[0]) {
            XUtils.toast().show("手机号码格式不正确")
            return
        }
        volunteer.phone = phoneNum

        // 性别
        volunteer.sex = findViewById<RadioButton>(sexRadioGroup.checkedRadioButtonId).text.toString()

        volunteer.orgId = teamId

        showLoadingDialog("正在加入")
        ziYuanZheVM!!.zyzAddUserInfo(volunteer)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<Boolean>) {

        if ("zyzAddUserInfo" != result.eventType) {
            return
        }

        hideLoadingDialog()
        when (result.code) {
            ResultInfo.SUCCESS -> {
                XUtils.toast().show("加入成功")
                finish()
            }
            ResultInfo.TOKEN_OVERDUE -> {
                // 不存在
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }
}